from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-45, -5, 0],
            [-35, 5, 0],
            [-25,-5,0],
            [-35, -15, 0],
            [-45,-5,0],
            
        ]
        self.set_points_smoothly(points)
class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-35, 5, 0],
            [-35,-5,0],
            [-45,-5,0],
            
        ]
        #self.set_points_smoothly(points)
        self.set_points_as_corners(points)#直线
class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-35, 5, 0],
            [-35,-5,0],
            [-45,-5,0],
            [-35, 5, 0],
            
        ]
        #self.set_points_smoothly(points)
        self.set_points_as_corners(points)#直线


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(张1)计算二重积分}$\iint _{D}\left| x^{2}+y^{2}-\sqrt{2}\left( x+y\right) \right| dxdy$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text0 = Tex(r"\text{,其中}$D=\left\{ \left( x,y\right)| x^{2}+y^{2}\leq 4\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,DOWN,buff=2)
        text1 = Tex(r"\text{(张2)计算二重积分}$\iint _{D}\left| x-\left| y\right| \right| d\sigma $\text{,其中}$D=\left\{  \left( x,y\right) | x^{2}+y^{2}\leq 2x,x\leq 1\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text2 = Tex(r"\text{(张3)计算二重积分}$\iint _{D}x\left| y-e^{x}\right| d\sigma $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        text20 = Tex(r"\text{,其中}$D=\left\{  \left( x,y\right) | 0\leq x\leq 1,0\leq y\leq 2e\right\} $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,DOWN,buff=2)
        
        self.play(Write(text1))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)


         # 创建坐标系
        axes = Axes(
            x_range=[0, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-10, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([-35, -5, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

    
       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve = CustomCurve(color=PURE_RED, stroke_width=20)
        curve.set_fill(color=RED, opacity=0.5)  # 绿色填充，50%透明度        
        self.play(Create(axes))
        self.play(  Create(curve))


         #下划线
        dot1=Dot([-35,5,0])
        dot2=Dot([-35,-15,0])
        line10=Line(dot1,dot2).set_color(PURE_GREEN)
        line10.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line10))
        text3 = Tex(r"$D$",color=PURE_BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([-40,-5,0])
        self.play(Create(text3))
        #下划线
        #下划线
        dot1=Dot([-20,16,0])
        dot2=Dot([-2,16,0])
        line100=Line(dot1,dot2).set_color(PURE_GREEN)
        line100.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line100))
        arrow = Arrow(start=[-10,14,0],end=[-10,9,0],color=YELLOW
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow))
        curve1 = CustomCurve1(color=PURE_GREEN, stroke_width=20)
        text4 = Tex(r"$2\iint _{D^{'}}\left| x-y\right| d\sigma $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,5,0])
        text5 = Tex(r"$D^{'}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([-40,0,0])
        text50 = Tex(r"$D^{'}$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([-40,0,0])
        self.play(Create(text4),ReplacementTransform(text3,text5), Create(curve1),FadeOut(line10))

        curve2 = CustomCurve2(color=PURE_GREEN, stroke_width=20)
        curve2.set_fill(color=GREEN, opacity=0.5)  # 绿色填充，50%透明度        
      
        self.play(  Create(curve2))

        text6 = Tex(r"$D1$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-37.5,-2.5,0])
        text7 = Tex(r"$D2$",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-42.5,2.5,0])
        self.play(
            ReplacementTransform(text5,text6),
            ReplacementTransform(text50,text7),
        )
        self.wait(0.2)

        text8 = Tex(r"$\Rightarrow 2\iint _{D1}\left( x-y\right) d\sigma -2\iint _{D2}\left( x-y\right) d\sigma $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=1)
        self.play(  Create(text8))

        dot1=Dot([5,1,0])
        dot2=Dot([20,1,0])
        line11=Line(dot1,dot2).set_color(PURE_GREEN)
        line11.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line11))

        dot1=Dot([25,1,0])
        dot2=Dot([42,1,0])
        line12=Line(dot1,dot2).set_color(PURE_GREEN)
        line12.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line12))


        text9 = Tex(r"$\iint _{D'}\left( x-y\right) d\sigma $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(  Create(text9))
        self.play(  FadeOut(text9))

        arrow = Arrow(start=[10,0,0],end=[-5,-5,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow))
        text10 = Tex(r"$=2\int _{0}^{1}dx\int _{0}^{x}\left( x-y\right) dy $",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-10,0])
        self.play(  Create(text10))
        text11 = Tex(r"$=2\int _{0}^{1}\dfrac{x^{2}}{2}dx=\dfrac{1}{3}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-15,0])
        self.play(  Create(text11))


        arrow = Arrow(start=[35,0,0],end=[35,-5,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow),self.camera.frame.animate.scale(1.2))

        text12 = Tex(r"$=2\int _{\dfrac{\pi }{4}}^{\dfrac{\pi }{2}}d\theta \int ^{2\cos \theta }_{0}r^{2}\left( \cos \theta -\sin \theta \right) dr$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-10,0])
        self.play(  Create(text12),self.camera.frame.animate.scale(1).shift(DOWN*10))
        text13 = Tex(r"$=2\int ^{\frac{\pi }{2}}_{\frac{\pi }{4}}\left( \cos \theta -\sin \theta \right) \cdot \int ^{2\cos \theta }_{0}r^{2}\cdot dr$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-20,0])
        self.play(  Create(text13),self.camera.frame.animate.scale(1).shift(DOWN*10))
        text14 = Tex(r"$=\dfrac{16}{3}\int ^{\frac{\pi }{2}}_{\frac{\pi }{4}}\left( \cos ^{4}\theta -\sin \theta \cos ^{3}\theta \right) d\theta $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-30,0])
        self.play(  Create(text14),self.camera.frame.animate.scale(1).shift(DOWN*10))
        dot1=Dot([37,-33,0])
        dot2=Dot([50,-33,0])
        line12=Line(dot1,dot2).set_color(PURE_GREEN)
        line12.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line12))
        text15 = Tex(r"$\dfrac{16}{3}\int _{\frac{\pi }{4}}^{\frac{\pi }{2}}\sin \theta \cos ^{3}\theta d\theta $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-45,-20,0])
        self.play(  Create(text15))
        text16 = Tex(r"$=\dfrac{-16}{3}\int _{\dfrac{\pi }{4}}^{\dfrac{\pi }{2}}\cos ^{3}\theta d\cos \theta $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-45,-30,0])
        self.play(  Create(text16))
        text17 = Tex(r"$=-\dfrac{16}{3}\left( \dfrac{1}{4}\cos ^{4}\theta \right) _{\frac{\pi }{4}}^{\frac{\pi }{2}}=\dfrac{1}{3}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-45,-40,0])
        self.play(  Create(text17))
        
        dot1=Dot([25,-33,0])
        dot2=Dot([34,-33,0])
        line12=Line(dot1,dot2).set_color(PURE_RED)
        line12.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line12))


        text18 = Tex(r"$\dfrac{16}{3}\int ^{\frac{\pi }{2}}_{\frac{\pi }{4}}\cos ^{4}\theta d\theta $",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-22,0])
        self.play(  Create(text18))

        arrow = Arrow(start=[-5,-24,0],end=[-5,-31,0],color=RED
                       , stroke_width=20, tip_length=1)
        self.play(GrowArrow(arrow),self.camera.frame.animate.scale(1))

        text19 = Tex(r"$\int \cos ^{4}\theta d\theta  $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-35,0])
        self.play(  Create(text19))

        text20 = Tex(r"$=\int \left( \dfrac{1+\cos ^{2}2\theta }{2}\right) ^{2}d\theta   $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-40,0])
        self.play(  Create(text20))
        text21 = Tex(r"$=\dfrac{1}{4}\int \left( 1+2\cos 2\theta +\cos ^{2}2\theta \right) d\theta    $",color=WHITE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-40,0])
        
        self.play(
            ReplacementTransform(text20,text21)
        )
        self.wait(0.2)

        dot1=Dot([0,-42,0])
        dot2=Dot([8,-42,0])
        line12=Line(dot1,dot2).set_color(PURE_RED)
        line12.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line12))

        text22 = Tex(r"$\cos ^{2}2\theta =\dfrac{1+\cos 4\theta }{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-50,0])
        self.play(  Create(text22))
        text23 = Tex(r"$=\dfrac{1}{4}\int \left( \dfrac{3}{2}+2\cos \cdot 2\theta +\dfrac{1}{2}\cos \cdot 4\theta \right) d\theta $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-40,0])
        
        self.play(
            ReplacementTransform(text22,text23),
            ReplacementTransform(line12,text23),
            ReplacementTransform(text21,text23)
        )

        text24 = Tex(r"$\dfrac{16}{3}\int ^{\dfrac{\pi }{2}}_{\dfrac{\pi }{4}}\cos ^{4}\theta d\theta = \dfrac{3}{8}\times \dfrac{\pi }{4}+\dfrac{1}{4}\sin 2\theta | _{\frac{\pi }{4}}^{\frac{\pi }{2}} +\dfrac{1}{32}\sin 4\theta | _{\frac{\pi }{4}}^{\frac{\pi }{2}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-5,-50,0])
        self.play(  Create(text24))
        text25 = Tex(r"$=\dfrac{\pi }{2}-\dfrac{4}{3}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text24,RIGHT,buff=1)
        self.play(  Create(text25))
        self.wait(0.2)

        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=30,height=10
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-45,-40,0])
        
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=7
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-5,-15,0])
        
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=7
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.next_to(text24,RIGHT,buff=1)
        self.play(Create(rectangle),Create(rectangle1),Create(rectangle2))
        text26 = Tex(r"$=2-\dfrac{\pi }{2}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text8,RIGHT,buff=1)
        self.play(self.camera.frame.animate.scale(1.5).move_to([0,-20,0]))
        self.play(
            ReplacementTransform(rectangle,text26),
            ReplacementTransform(rectangle1,text26),
            ReplacementTransform(rectangle2,text26)
        )
        self.play(self.camera.frame.animate.scale(0.5).move_to([35,0,0]))
        self.wait(2)
        
        





       